package com.btkjgc.controller;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.btkjgc.models.CartypeT;
import com.btkjgc.models.extend.CartypeModel;
import com.btkjgc.service.impl.CartypeManageService_Impl;
import com.btkjgc.service.intf.CartypeManageService_Intf;
import com.btkjgc.utils.HibernateUtil;

@Controller
public class CartypeManageController
{
	static Logger logger = Logger.getLogger(CartypeManageController.class);
	
	@Autowired
	private CartypeManageService_Intf cartypeManageService;
	
	@RequestMapping(value = "car_type_manage", method = { RequestMethod.GET,
			RequestMethod.POST })
	public ModelAndView getBusinessRecords(HttpServletRequest request) 
	{
		ModelAndView mv = new ModelAndView("car_type_manage");
		listrecord(request,mv);
		return mv;
	}
	
	@RequestMapping(value = "get_cartype_by_id")
	@ResponseBody
	public CartypeModel getCartypeById(Integer id) {
		
		String sql = "SELECT * FROM cartype_t WHERE id = ?";
		
		CartypeModel cartypeModel = (CartypeModel) HibernateUtil
				.executeSQLQueryObject(sql, CartypeModel.class, id);
		
		return cartypeModel;
	}
	
	@RequestMapping(value = "add_cartype", method = { RequestMethod.GET,
			RequestMethod.POST })
	public String addCartype(CartypeModel cartypeModel)
			throws IllegalAccessException, InvocationTargetException,
			NoSuchMethodException{
		
		System.out.println("AddCartype");
		CartypeT cartype = new CartypeT();
		cartype.setName(cartypeModel.getName());
		cartype.setDel(0);
		cartype.setCreatedate(new Date());
		cartype.setBattery_num(cartypeModel.getBattery_num());
		cartype.setSeat_num(cartypeModel.getSeat_num());
		cartype.setUnitprice(cartypeModel.getUnitprice());
		cartypeManageService.addCartype(cartype);
		
		return "redirect:/car_type_manage";
	}
	
	@RequestMapping(value = "edit_cartype", method = { RequestMethod.GET,
			RequestMethod.POST })
	public String editCartype(CartypeModel cartypeModel)
			throws IllegalAccessException, InvocationTargetException,
			NoSuchMethodException{
		
		System.out.println("EditCartype");
		cartypeManageService.updateCartype(cartypeModel);
		
		return "redirect:/car_type_manage";
	}

	@SuppressWarnings("unchecked")
	private void listrecord(HttpServletRequest request, ModelAndView mv) 
	{
		cartypeManageService = new CartypeManageService_Impl();
		
		String page = request.getParameter("page");
		
		int pageIndex = 0;
		int totalRecord = 0;
		if (page != null)
		{
			pageIndex = Integer.parseInt(page);
		}
		
		String sql = " SELECT * FROM cartype_t WHERE del=0 ORDER BY createdate";
		
		String count = " SELECT count(*) FROM cartype_t WHERE del=0";
		
		//获取分页查询的数据结果
		totalRecord = HibernateUtil.executeSQLQueryInt(count);
		
		List<CartypeModel> cartypeModel = HibernateUtil.executeSQLQueryList(sql, pageIndex, 15, null, CartypeModel.class);
		
		//设置后台用户界面所需的其他数据
		cartypeManageService.setListData(request,mv,pageIndex,totalRecord,cartypeModel);
	}
}